-
- #define F_CPU 6000000UL
- #define BAUD 9600
-
- #define UBRR F_CPU/16/BAUD-1
-
- #define DD_MOSI PB3
- #define DD_MISO PB5
- #define DD_SCK PB5
- #define DDR_SPI DDRB
- #define SS PB2
-
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
-
-
- void SPI_SlaveInit(void);
- char SPI_SlaveReceive(void);
- void usart_init(unsigned int);
- void usart_tx(unsigned char);
-
- volatile uint8_t plant[] = {0x41,0x42,0x43,0x44,0x45,0x46};
-
-
- /* Limits
- * Low Normal
- * Chili 3.83V 2.85V
- *
- * */
-
- int main()
- {
- ADMUX = (1<<REFS0)|(1<<ADLAR); // Set left-adj, AREF as voltage reference and use channel 0
-
- usart_init(UBRR);
- SPI_SlaveInit();
-
- //ADCSRB = ~(7<<ADTS0);
- //ADCSRA = (1<<ADEN)|(1<<ADATE)|(1<<ADIE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); // prescaler 128
- ADCSRA = (1<<ADEN)|(1<<ADIE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); // prescaler 128
-
-
- sei();
- while(1)
- {
- // ADCSRA |= (1<<ADSC);
- _delay_ms(1000);
- }
- }
-
- void usart_init(unsigned int baud)
- {
- UBRR0H = (unsigned char)(baud>>8);
- UBRR0L = (unsigned char)baud;
- UCSR0B = (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0);
-
- // Setting up frame-format
- UCSR0C = (1<<UCSZ01)|(1<<UCSZ00); // 8 data-bits
- UCSR0C |= (1<<USBS0); // 2 stop-bits
- UCSR0C |= (1<<UPM01); // parity even
-
- }
-
- void usart_tx(unsigned char data)
- {
- while (!(UCSR0A & (1<<UDRE0)))
- ;
- UDR0 = data;
- }
-
- void SPI_SlaveInit(void)
- {
- DDR_SPI = (1<<1)|(1<<DD_MISO); // MISO is output / PINB0 for debugging
- SPCR = (1<<SPIE)|(1<<SPE); // enable SPI and activate interrupt
- }
-
- ISR(SPI_STC_vect)
- {
- if (SPDR == 0x20)
- {
- PORTB |= (1<<1); // switch ON LED when sent value equals 0x20
- SPDR = 0xff;
- }
- else if (SPDR == 0x21)
- {
- PORTB &= ~(1<<1); // switch OFF LED when sent value equals 0x21
- SPDR = 0xee;
- }
- }
-
- ISR(ADC_vect)
- {
- usart_tx(0x0A);
- usart_tx(0x0D);
- }
-